package com.xcc.httpUtils.utils;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Tina on 10/14/2017.
 */
public class GsonUtil {

    private static final String JSON_CODE = "code";

    private static Gson gson = new Gson();

    /**
     * 转成json
     */
    public static String GsonString(Object object) {
        String gsonString = null;
        if (gson != null) {
            gsonString = gson.toJson(object);
        }
        return gsonString;
    }

    /** 避免不是小数点的值转出来是小数点 */
    public static String GsonToStrings(Object object) {
        Gson gson = new GsonBuilder().registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
            @Override
            public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
                if (src == src.longValue()) {
                    return new JsonPrimitive(src.longValue());
                }
                return new JsonPrimitive(src);
            }
        }).create();
        return gson.toJson(object);
    }

    /**
     * 转成bean
     */
    public static <T> T GsonToBean(String gsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gsonString, cls);
        }
        return t;
    }

    /**
     * 转成list
     */
    public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {
        List<T> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
            }.getType());
        }
        return list;
    }

    /**
     * 自定义类型转换
     * 例：MessageBean&lt;String, User&gt; msgBean = GsonUtil.fromJson( str, new TypeToken&lt;MessageBean&lt;String, User&gt;&gt;(){}.getType());
     * @param type 例：MessageBean&lt;String, User&gt; msgBean = GsonUtil.fromJson( str, GsonUtil.type(MessageBean.class, String.class, User.class));
     */
    public static <T> T fromJson(String gsonString, Type type) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gsonString, type);
        }
        return t;
    }

    /**
     * 获取JSON序列化类型，例：MessageBean&lt;IMSCameraInfo, Object&gt; msgBean = GsonUtil.fromJson( jsonStr, GsonUtil.type(MessageBean.class, IMSCameraInfo.class, Object.class));
     * @param raw 目标类
     * @param args 按目标类里声明的泛型属性之顺序来指定泛型属性的具体类型
     */
    public static Type type(final Class raw, final Type... args) {
        return new ParameterizedType() {
            public Type getRawType() {
                return raw;
            }

            public Type[] getActualTypeArguments() {
                return args;
            }

            public Type getOwnerType() {
                return null;
            }
        };
    }

    /**
     * 转成list 解决泛型问题
     */
    public static <T> List<T> jsonToList(String json, Class<T> cls) {
        Gson gson = new Gson();
        List<T> list = new ArrayList<T>();
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for (final JsonElement elem : array) {
            list.add(gson.fromJson(elem, cls));
        }
        return list;
    }


    /**
     * 转成list中有map的
     */
    public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
        List<Map<String, T>> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString,
                    new TypeToken<List<Map<String, T>>>() {
                    }.getType());
        }
        return list;
    }

    /**
     * 转成map的
     */
    public static <T> Map<String, T> GsonToMaps(String gsonString) {
        Map<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());
        }
        return map;
    }
}
